#!/usr/bin/env bash
# done 文件
rm -f /home/services/truely-plog/logs/start_script.done

# 环境判定
env=$1

# *.jar包所在地址
cd /home/services/truely-plog/bin

# 环境判定
if [ -z '${env}' ];
      then ${env}='dev'
fi

# jacoco端口
if [ "$JACOCO_PORT" != "" ]; then
     export JAVA_OPT=" -javaagent:/opt/jacoco/lib/jacocoagent.jar=output=tcpserver,address=0.0.0.0,port=16031"
fi

# java启动参数
JAVA_OPT="${JAVA_OPT}  -Xms1g -Xmx1g
                -XX:MetaspaceSize=2g
                -XX:MaxMetaspaceSize=2g
                -XX:+PrintGCDetails
                -Xloggc:../logs/gc.log
                -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps
                -XX:+UseG1GC
                -Dspring.profiles.active=${env}
                -jar *.jar"
PROG=$JAVA_HOME/bin/java
$PROG ${JAVA_OPT} 1>/dev/null 2>&1 &

if [ ${env} == "prod" ] || [ ${env} == "pref" ]; then
   chmod go-w /home/services/filebeat-log-${env}.yml
   sed -i "s/env: 'test'/env: '${env}'/g" filebeat-log-${env}.yml
   nohup /filebeat-6.3.1-linux-x86_64/filebeat -c /home/services/filebeat-log-${env}.yml &
fi



# 端口检查
port=8080
while true; do
                  s=`netstat -ntl | grep ":$port " -c`
                  echo $s
                  if [ "X$s" != "X" ] && [ "$s" != "0" ]; then
                      break
                  fi
                  sleep 5;
          done
# done 文件
touch /home/services/truely-plog/logs/start_script.done